package com.jc.common.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

public class Base64Util {

	/**
	 * @param bytes
	 * @return
	 */
	public static String decode(final String string) {
		try {
			return new String(Base64.decodeBase64(string.getBytes("UTF-8")), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 二进制数据编码为BASE64字符串
	 * 
	 * @param bytes
	 * @return
	 * @throws Exception
	 */
	public static String encode(final String string) {
		try {
			return new String(Base64.encodeBase64(string.getBytes("UTF-8")), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		String str = "{" + "\"productId\": 71," + "\"lon\": 121.353068," + "\"lat\": 31.24239" + "}";
		String b64str = encode(str);
		String mingstr = decode(b64str);
		System.out.println(b64str + "  ----------------  " + mingstr);
	}
}